# -*- coding: utf-8 -*-
# @Time      : 2025-08-01
# @Author    : 2023090007@ahdy.edu.cn
# @File      : service
# @Desc  
import argparse
import configparser
from common.loghandler import init_logger
from business import get_biz
from common.system import System

class Service(object):
    """
    服务层
    """
    
    options = None
    biz = None
    worker_process_num = 1
    master_msg_num = 1
    log_path = None
    log_level = None




    def __init__(self):
        pass 


    def initialize(self):
        Service.options = dict()
        self.parse_config() #配置文件解析
        self.parse_cmd() #命令行解析
        init_logger(Service.log_level, Service.log_path) #日志初始化
        Service.biz = get_biz(Service.biz)
        System.InitOptions(Service.options)


    def parse_cmd(self):
        """
        命令行解析
        """
        parser = argparse.ArgumentParser(description='command-line arguments')
        # 可选参数（--开头，可带短选项 -）
        parser.add_argument('-b', '--biz', type=str, 
                            help='biz name')
        parser.add_argument('-w', '--worker', type=int, 
                            help='worker process num')

        parser.add_argument('-c', '--config', type=str, 
                            help='config filename')

        parser.add_argument('-n', '--num', type=int,
                            help='master msg num')

        args = parser.parse_args()
        if args.biz is not None:
            Service.options['biz_name'] = args.biz
        if args.worker is not None:
            Service.options["worker_process_num"] = args.worker
        if args.config is not None:
            Service.options["config_filename"] = args.config
        if args.num is not None:
            Service.options['master_msg_num'] = args.num
        

    def parse_config(self):
        """
        配置文件解析
        """
        config = configparser.ConfigParser()
        config_filename = Service.options.get("config_filename", "dev.ini")
        config.read('dev.ini', encoding='utf-8')
        Service.biz = config.get("default", "biz")
        Service.worker_process_num = config.getint('default', 'worker_process_num')
        Service.master_msg_num = config.getint('default', 'master_msg_num')

        log_level = config.get("log", "level")
        log_path = config.get("log", "path")

        Service.log_level = log_level
        Service.log_path = log_path

        # 解析Mysql配置
        db_config = dict()
        db_config['host'] = config.get("database", "host")
        db_config['port'] = config.getint("database", "port")
        db_config['user'] = config.get('database', 'user')
        db_config['password'] = config.get('database', 'password')
        db_config['db_name'] = config.get('database', 'db_name')
        Service.options['db_config'] = db_config
        # 解析Redis配置
        redis_config = dict()
        redis_config['host'] = config.get("redis", "host")
        redis_config['port'] = config.getint("redis", "port")
        Service.options['redis_config'] = redis_config

        

